package de.oszimt.matse.messenger.dao;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import de.oszimt.matse.messenger.User;

/**
 * Verwendung von HSQLDB
 * 
 * Anmeldedaten stehen in der application-Context.xml
 * 
 * Servermodus starten:
 * java -cp hsqldb.jar org.hsqldb.Server -database.0 file:db/testdb -dbname.0 xdb
 * 
 * Datenbank-Manager starten: 
 * java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing
 * 
 * Anlegen neuer Benutzer in der Datenbank:
 * 
 * INSERT INTO USERS VALUES('user','pwd',TRUE); 
 * INSERT INTO AUTHORITIES VALUES('user','ROLE_USER');
 *
 */


public class UserDAOImplDB implements UserDAOInterface
{
	public JdbcTemplate jdbcTemplate;
	
	//TODO####################################################################
	private Hashtable<String, String> db = new Hashtable();
	private ArrayList<String> contactsOfA = new ArrayList<String>();
	private ArrayList<String> contactsOfIgor = new ArrayList<String>();
	private ArrayList<String> contactsOfAnton = new ArrayList<String>();
	//TODO####################################################################
	
	public UserDAOImplDB(DataSource dataSource) 
	{
		this.jdbcTemplate = new JdbcTemplate(dataSource);
		List<Map<String, Object>> result = this.jdbcTemplate.queryForList("SELECT * FROM Tabelle");
		System.out.println(result);
		//TODO####################################################################
		this.db.put("a", "a");
		this.db.put("anton", "a");
		this.db.put("bert", "a");
		this.db.put("caesar", "a");
		this.db.put("dennis", "a");
		this.db.put("frieda", "a");
		this.db.put("gudrun", "a");
		this.db.put("helene", "a");
		this.db.put("igor", "a");
		this.db.put("jochen", "a");
		this.db.put("kim", "a");
		this.db.put("laura", "a");
		contactsOfA.add("frieda");
		contactsOfA.add("laura");
		contactsOfA.add("bert");
		contactsOfA.add("jochen");
		contactsOfA.add("igor");
		contactsOfIgor.add("a");
		contactsOfIgor.add("frieda");
		contactsOfIgor.add("laura");
		contactsOfIgor.add("bert");
		contactsOfIgor.add("jochen");
		//TODO####################################################################
	}
	
	//TODO####################################################################
	@Override
	public boolean containsContactList(String userName, String newUser) 
	{
		if ("a".equals(userName)) return contactsOfA.contains(newUser);
		if ("igor".equals(userName)) return contactsOfIgor.contains(newUser);
		if ("anton".equals(userName)) return contactsOfAnton.contains(newUser);
		return false;
	}

	@Override
	public boolean existsUser(String user) 
	{
		// TODO Auto-generated method stub
		return this.db.containsKey(user);
	}

	@Override
	public void extendContact(User user, String newUser) 
	{
		// TODO Auto-generated method stub
		if ("a".equals(user.getName())) contactsOfA.add(newUser);
		if ("igor".equals(user.getName())) contactsOfIgor.add(newUser);
		if ("anton".equals(user.getName())) contactsOfAnton.add(newUser);
		//####################################################################
		if ("a".equals(newUser)) contactsOfA.add(user.getName());
		if ("igor".equals(newUser)) contactsOfIgor.add(user.getName());
		if ("anton".equals(newUser)) contactsOfAnton.add(user.getName());
	}

	@Override
	public ArrayList<String> getContactList(User user) 
	{
		if ("a".equals(user.getName())) return contactsOfA;
		if ("igor".equals(user.getName())) return contactsOfIgor;
		if ("anton".equals(user.getName())) return contactsOfAnton;
		return new ArrayList<String>();
	}

	@Override
	public boolean isPasswordCorrect(String user, String password) 
	{
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void reduceContact(User user, String userToRemove) 
	{
		// TODO Auto-generated method stub
		if ("a".equals(user.getName())) contactsOfA.remove(userToRemove);
		if ("igor".equals(user.getName())) contactsOfIgor.remove(userToRemove);
		if ("anton".equals(user.getName())) contactsOfAnton.remove(userToRemove);
		//####################################################################
		if ("a".equals(userToRemove)) contactsOfA.remove(user.getName());
		if ("igor".equals(userToRemove)) contactsOfIgor.remove(user.getName());
		if ("anton".equals(userToRemove)) contactsOfAnton.remove(user.getName());
	}

	@Override
	public boolean registerUser(String user, String password) 
	{
		// TODO Auto-generated method stub
		this.db.put(user, password);
		return true;
	}

	@Override
	public boolean validateUserData(String user, String password) 
	{
		// TODO Auto-generated method stub
		return password.equals(this.db.get(user));
	}
	//TODO####################################################################
}
